<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

interface Department {
    id: number;
    name: string;
}

interface Category {
    id: number;
    name: string;
}

const router = useRouter();
const categories = ref<Category[]>([]);
const departmentsByCategory = ref<Map<number, Department[]>>(new Map());

const fetchCategories = async () => {
    try {
        const response = await axios.get('http://127.0.0.1:8000/doctor/category/');
        categories.value = response.data;
        await Promise.all(
            categories.value.map(category => fetchDepartments(category.id))
        );
    } catch (error) {
        console.error('Error fetching categories:', error);
    }
};

const fetchDepartments = async (categoryId: number) => {
    try {
        const response = await axios.get(`http://127.0.0.1:8000/doctor/department/${categoryId}/`);
        departmentsByCategory.value.set(categoryId, response.data);
    } catch (error) {
        console.error(`Error fetching departments for category ${categoryId}:`, error);
    }
};

const navigateToDoctors = (departmentId: number) => {
    router.push(`/doctors/${departmentId}`);
};

onMounted(() => {
    fetchCategories();
});

// Department icons mapping
const departmentIcons: { [key: string]: string } = {
    '皮肤科': '🧴',
    '儿科': '👶',
    '妇产科': '👩',
    '消化内科': '🏥',
    '泌尿外科': '🚽',
    '骨科': '🦴',
    '口腔科': '🦷',
    '耳鼻咽喉科': '👂',
    '眼科': '👁️',
    // Add more icons as needed
};
</script>

<template>
    <div class="sections">
        <div v-for="category in categories" :key="category.id" class="category-section">
            <h2 class="category-title">{{ category.name }}</h2>
            <div class="departments-grid">
                <div v-for="department in departmentsByCategory.get(category.id)" :key="department.id"
                    class="department-item" @click="navigateToDoctors(department.id)">
                    <div class="department-icon">
                        <span class="icon">{{ departmentIcons[department.name] || '🏥' }}</span>
                    </div>
                    <div class="department-name">{{ department.name }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.sections {
    padding: 20px;
    background-color: #f5f5f5;
    min-height: 100vh;
}

.category-section {
    background-color: #ffffff;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.category-title {
    font-size: 18px;
    color: #333;
    margin-bottom: 20px;
    padding-left: 10px;
    border-left: 4px solid #00b38a;
}

.departments-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
}

.department-item {
    background-color: #fff;
    border-radius: 12px;
    padding: 15px;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 1px solid #f0f0f0;
}

.department-item:hover {
    background-color: #00b38a;
    /* Green background on hover */
    color: #ffffff;
    /* White text on hover */
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border-color: #00b38a;
}

.department-icon {
    margin-bottom: 12px;
}

.icon {
    font-size: 32px;
    color: #00b38a;
}

.department-item:hover .icon {
    color: #ffffff;
    /* White icon on hover */
}

.department-name {
    font-size: 14px;
    color: #333;
    font-weight: 500;
    transition: color 0.3s ease;
    /* Smooth transition for text color */
}
.department-item:hover .department-name {
    color: #ffffff;
    /* White text on hover */
}

@media (max-width: 768px) {
    .departments-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 480px) {
    .departments-grid {
        grid-template-columns: 1fr;
    }

    .sections {
        padding: 10px;
    }

    .category-section {
        padding: 15px;
    }
}
</style>